import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import MyFilm from '../views/MyFilm.vue'
import NowPlaying from '../views/NowPlaying.vue'
import ComingSoon from '../views/ComingSoon.vue'
import MyDetail from '../views/MyDetail.vue'


import MyCinema from '../views/MyCinema.vue'
import MyCitys from '../views/MyCitys.vue'


import MyNews from '../views/MyNews.vue'
import MyCenter from '../views/MyCenter.vue'

import MyLogin from '../views/MyLogin.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/film"
  }, {
    path: '/film',
    name: 'film',
    component: MyFilm,
    redirect: '/film/nowplaying',
    children: [
      {
        path: "nowplaying",
        component: NowPlaying
      },
      {
        path: "comingsoon",
        component: ComingSoon
      }
    ]
  }, {
    path: '/detial/:id',
    name: 'detial',
    component: MyDetail
  }, {
    path: '/cinema',
    name: 'cinema',
    component: MyCinema
  }, {
    path: '/citys',
    name: 'citys',
    component: MyCitys
  }, {
    path: '/news',
    name: 'news',
    component: MyNews
  }, {
    path: '/center',
    name: 'center',
    component: MyCenter
  }, {
    path: '/login',
    name: 'login',
    component: MyLogin
  },

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {

  // 需要登录才能访问的 路由地址集合
  const arr = [
    '/cinema'
  ]
  if (!arr.includes(to.path)) {
    next();
  } else {
    if (localStorage.getItem("token")) {
      next();
    } else {
      next({
        path: '/login',
        query: {
          refer: to.fullPath
        }
      })
    }
  }
})

export default router
